﻿using Leaf.NetCore.Cache;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;

namespace Leaf.NetCore.HotUpdate
{
    /// <summary>
    /// 与sqlconfig.xml关联业务类
    /// </summary>
    public class IISXmlBLL
    {
        private static string GetConf()
        {
            return AppDomain.CurrentDomain.BaseDirectory + "/iisconfig.xml";
        }

        /// <summary>
        /// 获取sqlconfig缓存数据，如果文件被修改过，重新获取
        /// </summary>
        /// <returns></returns>
        public static List<IISXmlModel> GetModelListFromCache()
        {
            string filePath = GetConf();
            if (!File.Exists(filePath))
            {
                return null;
            }
            List<IISXmlModel> result = null;
            FileInfo file = new FileInfo(filePath);
            DateTime lastTime = file.LastWriteTime;

            IISMainXML modelList = CacheHelper.GetCache<IISMainXML>("iisconfig");
            if (modelList == null)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);

                IISMainXML obj = XMLHelper.XmlDeserialize<IISMainXML>(doc.InnerXml);
                obj.lastTime = lastTime;
                CacheHelper.SetCache<IISMainXML>("iisconfig", obj);
                result = obj.info;
            }
            else
            {
                var temp = (IISMainXML)modelList;
                if (temp.lastTime == lastTime)
                {
                    result = temp.info;
                }
                else
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(filePath);
                    IISMainXML obj = XMLHelper.XmlDeserialize<IISMainXML>(doc.InnerXml);
                    obj.lastTime = lastTime;
                    CacheHelper.SetCache("iisconfig", obj);
                    result = obj.info;
                }
            }
            return result;
        }

        public static IISXmlModel GetInfoByName(string name)
        {
            var modelist = GetModelListFromCache();
            var item = modelist.Where(p => p.name == name).ToList().FirstOrDefault();
            return item;
        }

        public static List<string> GetIISXmlListName()
        {
            List<string> result = new List<string>();
            var modelList = GetModelListFromCache();
            foreach (var model in modelList)
            {
                result.Add(model.name);
            }
            return result;
        }

        public static bool SaveItemXML(IISXmlModel model, string path = "")
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "/sqlconfig.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);

            return true;
        }

        public static bool AddItem(IISXmlModel model, string index)
        {
            string filePath = GetConf();
            try
            {
                XMLHelper.XmlInsertValue(filePath, "/iis", "info", "", "");
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + index + "]", "id", index);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "id", "", index);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "name", "", model.name);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "iisname", "", model.iisname);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "iispath", "", model.iispath);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "ftppath", "", model.ftppath);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "exclude", "", model.exclude);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "pubdate", "", model.pubdate);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "pubstate", "", model.pubstate);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "watcher", "", model.watcher);
                XMLHelper.XmlInsertValue(filePath, "/iis/info[@id='" + index + "']", "second", "", model.second);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool EditItem(IISXmlModel model)
        {
            string filePath = GetConf();
            try
            {
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/name", "", model.name);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/iisname", "", model.iisname);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/iispath", "", model.iispath);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/ftppath", "", model.ftppath);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/exclude", "", model.exclude);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/watcher", "", model.watcher);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/second", "", model.second);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/pubdate", "", model.pubdate);
                XMLHelper.XmlUpdateValue(filePath, "/iis/info[" + model.id + "]/pubstate", "", model.pubstate);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}